package me.zhengjie.modules.management.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.management.domain.MrAttention;
import me.zhengjie.modules.management.domain.MrNotice;
import me.zhengjie.modules.management.mapper.MrAttentionMapper;
import me.zhengjie.modules.management.service.MrAttentionService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @Author: chenxin.jiang
 * @Date: 2020/12/15
 * @Description:
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MrAttentionServiceImpl implements MrAttentionService {

private final MrAttentionMapper mrAttentionMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAttention(MrAttention mrAttention) {
        try{
            //mrAttention.setAttention(mrAttention.getAttention());
            mrAttention.setCreateDateTime(new Timestamp(new Date().getTime()));
            mrAttention.setCreateUserId(SecurityUtils.getCurrentUsername());
            mrAttentionMapper.addAttention(mrAttention);
        }catch(Exception e){
            throw new BadRequestException("新增操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAttention(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                mrAttentionMapper.deleteAttention(id);
            }
        } catch (RuntimeException e) {
            throw new BadRequestException("删除操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAttention(MrAttention mrAttention) {
        try {
            mrAttention.setAttention(mrAttention.getAttention());
            mrAttention.setModifyDateTime(new Timestamp(new Date().getTime()));
            mrAttention.setModifyUserId(SecurityUtils.getCurrentUsername());
            mrAttentionMapper.updateAttention(mrAttention);
        } catch (RuntimeException e) {
            throw new BadRequestException("更新操作失败！");
        }
    }

    @Override
    public List<MrAttention> queryAttention() {
        try {
            List<MrAttention> list=mrAttentionMapper.queryAttention();
            return list;
        } catch (RuntimeException e) {
            throw new BadRequestException("查询操作失败！");
        }
    }
}
